package cn.rdq.iknessplatform.config;

import cn.rdq.common.util.support.FileConst;
import cn.rdq.iknessplatform.interceptor.LoginInterceptor;
import cn.rdq.iknessplatform.interceptor.OnlineInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    private String path = System.getProperty("user.home");

    //静态资源配置 图片等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String workDir = FileConst.FILE_PROTOCOL + FileConst.WORK_DIR + FileConst.FILE_SEPARATOR;
        registry.addResourceHandler("/upload/**")
                .addResourceLocations(workDir + "upload/");
    }

    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Bean
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new OnlineInterceptor()).addPathPatterns("/**").excludePathPatterns("/status/**");
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**/update","/**/user/**","/**/del/**","/**/delete/**")
                .excludePathPatterns("/user/login","/user/register");
    }
}
